<template>
  <div class="all-products">
    <div class="header">
      <h1>全部商品</h1>
      <div class="sort-options">
        <label for="sort-select">排序方式:</label>
        <select id="sort-select" v-model="selectedSort" @change="sortProducts">
          <option value="default">默认</option>
          <option value="price-asc">价格升序</option>
          <option value="price-desc">价格降序</option>
          <option value="name-asc">名称 A-Z</option>
          <option value="name-desc">名称 Z-A</option>
        </select>
      </div>
    </div>
    <div class="product-grid">
      <div v-for="product in displayedProducts" :key="product.id" class="product-card">
        <router-link :to="{ name: 'ProductDetail', params: { id: product.id } }" class="product-link">
          <div class="product-image">
            <img :src="product.image" :alt="product.name" />
          </div>
          <div class="product-info">
            <h3>{{ product.name }}</h3>
            <p class="price">¥{{ product.price.toFixed(2) }}</p>
          </div>
        </router-link>
      </div>
    </div>
  </div>
</template>
<script setup>
import { ref, onMounted, computed } from 'vue';
import { useProductStore } from '../stores/productStore';
const productStore = useProductStore();
const products = ref([]);
const selectedSort = ref('default');
const displayedProducts = computed(() => {
  if (selectedSort.value === 'default') {
    return products.value;
  } else if (selectedSort.value === 'price-asc') {
    return [...products.value].sort((a, b) => a.price - b.price);
  } else if (selectedSort.value === 'price-desc') {
    return [...products.value].sort((a, b) => b.price - a.price);
  } else if (selectedSort.value === 'name-asc') {
    return [...products.value].sort((a, b) => a.name.localeCompare(b.name));
  } else if (selectedSort.value === 'name-desc') {
    return [...products.value].sort((a, b) => b.name.localeCompare(a.name));
  }
});
onMounted(() => {
  products.value = productStore.products;
});
const sortProducts = () => {
  // 排序逻辑在 computed 属性中处理,这里不需要额外的操作
};
</script>
<style scoped>
.all-products {
  max-width: 1200px;
  margin: 2rem auto;
  padding: 0 1rem;
}
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 2rem;
  padding: 1rem;
  background-color: #f5f5f5;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
h1 {
  font-size: 2.5rem;
  color: #333;
}
.sort-options {
  display: flex;
  align-items: center;
}
.sort-options label {
  margin-right: 1rem;
  font-size: 1.1rem;
  color: #666;
}
.sort-options select {
  padding: 0.5rem 1rem;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 1rem;
  outline: none;
  background-color: #fff;
  color: #333;
  cursor: pointer;
  transition: border-color 0.3s ease;
}
.sort-options select:hover {
  border-color: #aaa;
}
.product-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 2rem;
}
.product-card {
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  transition: transform 0.3s ease, box-shadow 0.3s ease;
}
.product-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);
}
.product-link {
  display: block;
  text-decoration: none;
  color: inherit;
}
.product-image {
  position: relative;
  overflow: hidden;
  padding-top: 100%;
}
.product-image img {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.3s ease;
}
.product-card:hover .product-image img {
  transform: scale(1.1);
}
.product-info {
  padding: 1rem;
  text-align: center;
}
h3 {
  font-size: 1.25rem;
  margin-bottom: 0.5rem;
  color: #333;
}
.price {
  font-size: 1.5rem;
  font-weight: bold;
  color: #e44d26;
}
</style>
